# numpy库的应用
import numpy as np
from numpy.core.fromnumeric import size
# c语言  Python语言运行时有线程锁，无法实现真正的多线程并行，而C语言可以
from numpy.core.function_base import logspace
from numpy.random import shuffle


def compute_red(values):
    res = []
    for value in values:
        res.append(1/value)
    return res


values = list(range(1, 1000000))

# %timeit compute_red(values)   ipython中统计运行时间的魔数方法（多次运行求平均值）

# values =np.arange(1,1000000)


'''
1.数组的创建
'''
# x=np.array([1,2,3,4,5],dtype=float)
# # x=np.array([1,2,3,4,5])
# print(x)
# print(type(x))
# print(type(x[0]))
# print(x.shape)

# 二维数组
# x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# print(x)
# print(x.shape)

# 从头创建数组
# 创建长度为5的数组，值都为0
# x=np.zeros(5,dtype=int)
# print(x)

# 创建2*4的浮点型数字，值为1
# x= np.ones((2,4),dtype=float)
# print(x)

# 创建一个3*5的数组，值都为8.8
# x=np.full((3,5),8.8)
# print(x)

# 创建一个3*3的单位矩阵
# x= np.eye(3)
# print(x)


# 创建一个线性序列数组，从1开始，步长为2,结束为15
# x = np.arange(1, 15, 2)
# print(x)

# 创建一个4个元素的数组，这四个数均匀的分配到0-1
# x=np.linspace(0,1,4)
# print(x)

# 创建一个10个元素的数组，行程一个1~10^9的等比数列
# x = logspace(0, 9, 10)
# print(x)


# 创建一个3*3的，在0~1之间均匀分布的随机数构成的数组
# x=np.random.random((3,3))
# print(x)


# 创建一个3*3的，均值为0，标准差为1的随机数构成的数组
# x=np.random.normal(0,1,(3,3))
# print(x)

# 创建一个3*3的，在[0,10)之间随机整数构成的数组
# x=np.random.randint(0,10,(3,3))
# print(x)


# 随机重排序

# x =np.array([10,20,30,40])
# x1=np.random.permutation(x) #生产新列表
# np.random.shuffle(x) #改变原列表
# print(x)
# print(x1)


'''
随机采样
'''
# 按指定形状采样
# x = np.arange(10, 25, dtype=float)
# # print(x)

# x = np.random.choice(x,size=(4,3))
# print(x)


# 按概率采样
# x =np.random.choice(x,size=(4,3),p=x/np.sum(x))
# print(x)


'''
numpy数组的性质
'''
# x = np.random.randint(10,size=(3,4))  #3行4列
# # print(x)

# #数组的形状shape
# print(x.shape)

# #数组的维度ndim
# print(x.ndim)

# #数组的大小
# print(x.size)

'''
数组的索引
'''
# 一维数组的索引
# x=np.arange(10)
# print(x[0])
# print(x[-5])


# x=np.random.randint(0,20,(2,3))
# print(x)

# print(x[0,0])

# 一维数组切片与列表一样
# x=np.arange(10)
# print(x)
# print(x[:3])
# print(x[::-1])


# 二维数组切片
# x=np.random.randint(20,size=(3,4))
# print(x)

# print(x[:2,:3])  #前两行 前3列

# print(x[:2,0:3:2]) #前2行，前3列（每隔一列）
# print(x[::-1,::-1])

# 获取数组的行和列   切片获取的是视图，而非副本  视图元素发生变化，原数组发生变化
# x = np.random.randint(20, size=(3, 4))
# # print(x)

# # print(x[1, :])  # 第一行，从0开始计数

# # print(x[1])  # 第一行简写
# # print(x[:, 2])  # 第3列
# #copy
# print(x[:, 2].copy())  # 第3列


'''
数组的变形
'''
# x= np.random.randint(0,10,(12,))
# print(x)

# x=x.reshape(3,4)  #返回的是视图，而非副本

# print(x)

# 一维向量转行向量
# x1=x.reshape(1,x.shape[0])
# print(x)

# 一维向量转列向量


# 多维向量转一维向量
# x = np.random.randint(0, 10, (3, 4))
# print(x)
# # x2=x.flatten()  #返回的是副本
# x2 = x.reshape(-1)  # 返回的视图
# print(x2)


'''
数组的拼接
'''
# x1 = np.random.randint(0, 10, (3, 4))
# x2 = np.random.randint(0, 10, (3, 4))

# x3=np.hstack([x1,x2])  #水平拼接 非视图 更新x3 不改变原来数据
# x3=np.c_[x1,x2]   #水平拼接
# print(x3)

# 垂直拼接
# x3=np.vstack([x1,x2])
# x4=np.r_[x1,x2]
# print(x3)
# print(x4)

'''
数组的分裂
'''
# x = np.arange(10)
# x1, x2, x3 = np.split(x, [2, 7])
# print(x1, x2, x3)


# hsplit用法
# x = np.arange(1, 26).reshape(5, 5)
# print(x)
# left, mid, right = np.hsplit(x, [2, 4])
# print("left:\n", left)
# print("mid:\n", mid)
# print("right:\n", right)


# vsplit
# x = np.arange(1, 26).reshape(5, 5)
# print(x)
# upper, mid, lower = np.vsplit(x, [2, 4])
# print("left:\n", upper)
# print("mid:\n", mid)
# print("right:\n", lower)

'''
numpy 四大运算
'''

# 1.向量化运算
# a1 = np.arange(1, 6)
# print("a1+5", a1+5)
# print("a1-5", a1-5)
# print("a1*5", a1*5)
# print("a1/5", a1/5)
# print("-a1", -a1)
# print("a1**2", a1**2)
# print("a1//2", a1//2)
# print("a1%2", a1%2)

# 2.绝对值、三角函数、指数、对数
# 绝对值
# a2=np.array([1,-1,2,-2,0])
# print(abs(a2))

# a3=np.linspace(0,np.pi,3)  #等差数列
# print("sin",np.sin(a3))
# print("cos",np.cos(a3))
# print("tan",np.tan(a3))


# a4 = [1, 0, -1]
# print("arcsin(x)", np.arcsin(a4))
# print("arccos(x)", np.arccos(a4))
# print("arctan(x)", np.arctan(a4))


# 指数运算
# x=np.arange(3)
# print(x)

# print(np.exp(x))

# 对数运算
# x=np.array([1,2,4,8,16])
# print("ln(x)",np.log(x))
# print("log2(x)",np.log2(x))
# print("log10(x)",np.log10(x))


# 两个数组的运算
# x1=np.arange(1,6)
# x2=np.arange(6,11)
# print("x1+x2",x1+x2)
# print("x1-x2",x1-x2)
# print("x1*x2",x1*x2)
# print("x1/x2",x1/x2)


'''
矩阵运算
'''
# a=np.arange(9).reshape(3,3)
# print(a)

# y=a.T  #矩阵转置
# print(y)


# 矩阵乘法 区别： x*y是对应位置相乘
# x = np.array([[1, 0],
#              [1, 1]])
# y = np.array([[0, 1], [1, 1]])
# print(x.dot(y))


# 广播运算

# x=np.ones((3,3))
# x2=np.arange(3).reshape(1,3)
# print(x+x2)


# 比较运算
# x1=np.random.randint(100,size=(10,10))
# print(x1)
# print(x1>50)
# print(np.all(x1>0))
# print(np.any(x1>5))
# print(np.all(x1>6,axis=1)) #按行


# #将布尔数组作为掩码
# print(x1[x1>50])


# 花哨的索引
# 一维数组
# x=np.random.randint(100,size=10)
# ind=[2,6,8]
# print(x[ind])   #输出形状与索引形状一样

# index=np.array([[0,1],[1,0]])
# print(x[index])


# 多维数组
# x = np.arange(12).reshape(3, 4)
# print(x)
# row = np.array([0, 1, 2])
# col = np.array([1, 3, 0])
# print(x[row, col])  # x(0,1)  (1,3)  (2,0)


# print(row[:, np.newaxis])  # 列向量

# print(x[row[:, np.newaxis], col])  # 广播机制

#排序
# x=np.random.randint(20,50,size=10)
# # print(x)
# x=np.sort(x)
# print(np.argmax(x)) #最大值索引
# print(np.max(x)) #最大值
# print(np.sum(x))

# x=np.argsort(x)

# print(x)

# x=np.arange(6).reshape(2,3)
# print(x)
# #按行求和
# print(np.sum(x,axis=1))

# print(np.sum(x,axis=0))


#求积
# x=np.arange(6)
# print(x)
# print(np.prod(x))

#中位数、均值、方差、标准差
x=np.random.normal(0,1,size=10000)
import matplotlib.pyplot as plt
plt.hist(x,bins=50)
plt.show()

#中位数
print(np.median(x))
#均值
print(np.mean(x))
#均值
print(np.var(x))
#标准差
print(np.std(x))

#标准差
